<?php
//@app_header;

/**
 * pending on development
 *
 */
$core_dir = dirname(dirname(__file__));
$cjax_dir = dirname($core_dir);
require_once $core_dir.'/cjax_config.php';

class plugin {
	
	private static $plugins = array();
	private static $instance;
	public static $list;
	
	public static function readDir($resource,$read_directories = false)
	{
		$dir = scandir($resource);
		unset($dir[0],$dir[1]);	
		if(!$read_directories) {
			foreach($dir as $k => $v ) {
				if(is_dir($resource.DIRECTORY_SEPARATOR.$v)) {
					unset($dir[$k]);	
				}
			}
		}
		return $dir;
	}
	
	public static function getInstance()
	{
		if(isset(self::$instance)) {
			return self::$instance;
		}
		
		
		return self::$instance = new plugin;
	}
	
	/**
	 * get the full path of a plugin
	 */
	function file($name)
	{
		$plugin_name = self::$list[$name];
		
		return $plugin_name;
	}
	
	
	function exists($name)
	{
		if(isset(self::$list[$name])) {
			return true;
		}
	}
	
	function __construct() 
	{
		$ajax = CJAX::getInstance();
		$method = $ajax->get('method');
		$params = $ajax->get('params');
		
		$base = CJAX_HOME;    
		$plugins = CJAX_HOME."/plugins/"; 
		$file = $base.$method;
		
		$dir = self::readDir($plugins);
		foreach($dir as $k => $v) {
			self::$list[substr($v, 0,strlen($v)-3)]= $v;
		}
	}
	
	function log($log)
	{
		echo "\n\nalert(\"$log\");\n";
	}
	
}
